<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	function __construct() {
		parent::__construct();

		//Not required if you autoload the library
		$this->load->library('doctrine');

		//Instantiate a Doctrine Entity Manager
		$this->em = $this->doctrine->em;
	}

	public function index()
	{
		$data['ip_address'] = $this->input->ip_address();
		$data['user_agent'] = $this->input->user_agent();

		$this->load->view('welcome_message', $data);
		$this->output->enable_profiler(TRUE);
	}

	public function sendmail(){
		$this->load->library('email');

		$this->email->from('you@example.com', 'Your Name');
		$this->email->to('n.manhnguyen@gmail.com');
		$this->email->cc('another@person.com');
		$this->email->bcc('theboss@example.com');
		$this->email->subject('Email Test');
		$this->email->message('This is a simple test we wrote for the email class.');

		$this->email->send();

		//echo "Email send successful";

		echo $this->email->print_debugger();
	}

	public function contact(){
		$this->load->library('email');
		$this->load->helper('email');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('name', 'Name',  'required|xss_clean');
		$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email|callback_add_user|xss_clean');
		$this->form_validation->set_rules('subject', 'Subject', 'required|xss_clean');
		$this->form_validation->set_rules('message', 'Message', 'required|xss_clean');

		if($this->form_validation->run() == FALSE) {
			//load the contact form
			$this->load->view('contact');
		}
		else {
			//send the email				
			$name = $this->input->post('name');
			$email = $this->input->post('email');
			$subject = $this->input->post('subject');
			$message = $this->input->post('message');
				
			$this->email->from($email, $name);
			$this->email->to('nmanhnguyen@localhost.com');
				
			$this->email->subject($subject);
			$this->email->message($message);
				
			$this->email->send();
			
			$data['msg'] = "Thank you, your email has now been sent.";
			$this->load->view('email_success', $data);
			
			echo $this->email->print_debugger();
		}
	}
	
	function add_user($email){
		$this->load->database();
		
		$query = $this->db->query("SELECT * FROM `user_data` WHERE `email` = '$email'");
		
		if($query->num_rows() === 0) {
			$name  = $this->input->post('name');
			$this->db->query("INSERT INTO `user_data` (name, email) VALUES ('$name', '$email')");
		}
	}

	function benchmark() {

		// 		$this->benchmark->mark('tea');
		// 		// something happens here
		// 		$this->benchmark->mark('coffee');
		// 		// something else happens here
		// 		$this->benchmark->mark('biscuits');
		// 		echo $this->benchmark->elapsed_time('tea', 'coffee');
		// 		echo $this->benchmark->elapsed_time('coffee', 'biscuits');
		// 		echo $this->benchmark->elapsed_time('tea', 'biscuits');

		// 		$this->benchmark->mark('first_mark_start');
		// 		// something happens
		// 		$this->benchmark->mark('first_mark_end');
		// 		$this->benchmark->mark('second_mark_start');
		// 		// something else happens
		// 		$this->benchmark->mark('second_mark_end');

		echo "IP Address: " . $this->input->ip_address();


		$this->output->enable_profiler(TRUE);
	}

	function paging()
	{
		$this->load->library('pagination');

		$config['base_url'] = 'http://localhost:8080/projects/codeIgniter_2.1.0/index.php/welcome/paging';
		$config['total_rows'] = 200;
		$config['per_page'] = 20;

		$this->pagination->initialize($config);

		echo $this->pagination->create_links();
	}

	function session() {
		echo print_r($this->session->all_userdata());
	}

	function unit_test(){
		$this->load->library('unit_test');

		$test = 1 + 1;

		$expected_result = 2;

		$test_name = 'Adds one plus one';

		echo $this->unit->run($test, $expected_result, $test_name);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */